package com.itfreer.setting.configsyn;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.itfreer.gis.config.GisConfigManager;
import com.itfreer.gis.offline.OfflineSynService;
import com.itfreer.gis.styles.data.StyleEntity;
import com.itfreer.gis.styles.StyleService;
import com.itfreer.gis.utils.StyleUtils;
import com.itfreer.utils.MyProgressDialog;

import java.util.List;

/**
 * 配置同步辅助类
 */
public class ConfigSynUtils {

    private MyProgressDialog configSynDialog;
    private ConfigSynHandler configSynHandler;

    public ConfigSynUtils(android.content.Context context) {
        configSynDialog = new MyProgressDialog(context);
        configSynHandler = new ConfigSynHandler();
    }

    /**
     *
     * @param showMsg       是否显示对话框
     * @param qzUpdate      是否强制更新
     */
    public void configSyn(final Boolean showMsg, final Boolean qzUpdate){
        ConfigSynService.instance.synConfig(new ConfigSynService.ProgressListener(){

            @Override
            public void onSize(int count, int size, String s) {
                Message msg = new Message();
                Bundle b = new Bundle();
                b.putString("message", s);
                b.putInt("progress", size);
                msg.setData(b);
                configSynHandler.sendMessage(msg);
            }

            @Override
            public void onFail(String info) {
                Message msg = new Message();
                Bundle b = new Bundle();
                b.putString("message", info);
                msg.setData(b);
                configSynHandler.sendMessage(msg);
            }

            /**
             * 成功完成
             */
            @Override
            public void onSuccess() {
                Message msg = new Message();
                Bundle b = new Bundle();
                b.putString("message", "onSuccess");
                msg.setData(b);
                configSynHandler.sendMessage(msg);
            }
        }, showMsg, qzUpdate);
    }

    /**
     * 启动数据同步进度对话框
     */
    private void showConfigSynDialog(){
        // 设置对话框参数
        configSynDialog.setTitle("配置同步");
        configSynDialog.setMessage("启动配置同步");
        configSynDialog.setCancelable(false);
        // 设置进度条参数
        configSynDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        configSynDialog.setMax(9);
        configSynDialog.setProgress(0);
        configSynDialog.setIndeterminate(false); // 填false表示是明确显示进度的 填true表示不是明确显示进度的
        configSynDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(ConfigSynService.instance.getIsRuning()) return;
                configSynDialog.dismissManually();
            }
        });
        configSynDialog.show();
    }

    private class ConfigSynHandler extends Handler {
        public ConfigSynHandler() {
        }

        // 子类必须重写此方法，接受数据
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 此处可以更新UI
            Bundle b = msg.getData();
            String s = b.getString("message", "");
            if(s.equals("onSuccess")){
                // 同步成功后，切换地图方案
                final List<StyleEntity> styles = StyleService.instance.getEntitys();
                if(styles!=null && styles.size()>0){
                    StyleService.instance.changeStyle(styles.get(0), new StyleService.StyleChange() {
                        @Override
                        public void finsh() {
                            // 初始化参数
                            StyleUtils.setMapParams(styles.get(0), true);
                            // 启动时数据同步一次
                            OfflineSynService.instance.synData(false);
                        }
                    });
                } else {
                    GisConfigManager.setStyleID(null);
                }
                if(configSynDialog!=null) configSynDialog.dismissManually();
            } else {
                int size = b.getInt("progress", 0);
                if(size==1){
                    showConfigSynDialog();
                }
                configSynDialog.setProgress(size);
                configSynDialog.setMessage(s);
            }
        }
    }
}
